package pl.webshoptest;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

public class MainPage extends Page {
		private String logoutLinkPath = "//*[@id='wyloguj']";
		private String Item1AddButtonPath = "//*[@id='Add1']";
		private String Item2AddButtonPath = "/html/body/table/tbody/tr[3]/td[3]/input";
		private String Item1RemoveButtonPath = "//*[@id='Remove1']";
		private String Item2RemoveButtonPath = "/html/body/table/tbody/tr[3]/td[4]/input";
		private String goToCartPath = "/html/body/a";
		private String numberOfItemsPath = "/html/body/p";
		public static final int ITEM1 = 1;
		public static final int ITEM2 = 2;
		public static final String ADRESS = "localhost:8084/webshop/main.jsp";
		
		
        public MainPage(WebDriver driver) {
                super(driver);
        }
       
        public boolean isOpen() {
                try{
                	findElementByLinkText("wyloguj");               		
                } catch(NoSuchElementException e){
                        return false;
                }
                return true;
        }

        public void clickAddItem(int item) {
        	switch(item) {
        		case 1: clickXPath(Item1AddButtonPath);
        				break;
        		case 2: clickXPath(Item2AddButtonPath);
        				break;
        		default: break;
        	}
        }
        
        public void clickRemoveItem(int item) {
        	switch(item) {
        		case 1: clickXPath(Item1RemoveButtonPath);
        				break;
        		case 2: clickXPath(Item2RemoveButtonPath);
        				break;
        		default: break;
        	}
        }
        
        public CartPage clickGoToCart() {
        	clickXPath(goToCartPath);
        	return new CartPage(driver);
        }
        
        public int getNumberOfItems(){
        	return	 Integer.parseInt(findElement(numberOfItemsPath).getText());
        }
        
        public LoginPage logout(){           
            clickXPath(logoutLinkPath);
            return new LoginPage(driver);
        }
}

